package com.kaussoft.lolblog.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.kaussoft.lolblog.vo.Account;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;

public class AccountAction extends AbstractAction implements ServletResponseAware,  ServletRequestAware{

	private static final long serialVersionUID = 1L;
	private HttpServletResponse response;
	private HttpServletRequest request;

	public String execute() throws Exception {
		String callback = request.getParameter("callback");
		List<Account> accounts = getServiceManager().findAccounts(true);
		for (int i = 0; i < accounts.size(); i++) {
			accounts.get(i).setWblogs(null);
		}
		JSONArray jsonArray = JSONArray.fromObject(accounts); 
		response.setContentType("text/xml;charset=utf-8");
		String data = callback + "({accounts:" + jsonArray + "})";
		response.getWriter().print(data);
		response.getWriter().close();
		System.out.println(data);
		return null;
	}
	
	public String updateAccount() throws Exception {
		String name = request.getParameter("accountname");
		String pass = request.getParameter("accountpass");
		Account account = getServiceManager().findAccountsByName(name, true);
		if (account != null) {
			account.setAccountpass(pass);
			if (getServiceManager().updateAccount(account)) {
				response.setContentType("text/xml;charset=utf-8");
				response.getWriter().print("pass update success");
				response.getWriter();
			}
		}
		return null;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}


}
